<!-- 作者 : liandyao 公司: yidu -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>鲜花类型</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <link rel="stylesheet" href="/res/layui/css/layui.css">
    <script src="/res/layui/layui.js"></script>
</head>
<body>
<!-- 查询的元素 -->
<div class="layui-form-item" style="margin-top: 10px">
    <label class="layui-form-label">类型名称</label>
    <div class="layui-input-inline">
        <input type="text" id="flName" lay-verify="title" autocomplete="off" placeholder="请输入类型名称" class="layui-input">
    </div>


    <div class="layui-inline">
        <button type="button" class="layui-btn layui-btn-sm" id="queryBtn">查询</button>

    </div>
</div>

<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>

</script>
<script>
    var table = null ;
    layui.use(['table','laydate'], function(){
        table = layui.table;
        var laydate = layui.laydate;
        var $ = layui.$;


        table.render({
            elem: '#test'
            ,url:'/flType/showList'
            ,page: true //开启分页
            ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,totalRow: true //开启合计行
            ,id:'testTable'
            ,cols: [[
                {type:'checkbox'}

                ,{field:'typeName', width:120, title: '鲜花名称'}
                ,{field:'typeRemark', width:120, title: '价格', sort: true, totalRow: true }

                ,{fixed: 'right', width: 150, align:'center', toolbar: '#barDemo'}
            ]]

        });

        //查询按钮绑定事件
        $("#queryBtn").on("click",function () {
            //layer.alert("点击事件");
            //执行重载
            table.reload('testTable', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: { //刷新表格时,向后台传输的参数

                    typeName: $("#flName").val(), //类型名字


                }
            });
        });


        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'update'){
                //layer.msg('修改操作');

                layer.open({
                    type: 2,
                    title: '鲜花类型维护',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['580px', '90%'],
                    content: '/pages/flType/form.html?id='+data.typeId //iframe的url
                });

            } else if(layEvent === 'delete'){
                layer.confirm('真的删除行么', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);
                    //向服务端发送删除指令
                    let url = "/flType/delete";
                    let data_ = {id:data.typeId};//id从哪里来? data里面取
                    $.post(url,data_,function(resp){
                        if(resp.state==1){
                            layer.msg("删除成功!");
                        }else{
                            layer.msg("删除失败!");
                        }
                    },"json");

                });
            }
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    //layer.msg('添加');
                    //iframe层

                    var indexOpen = layer.open({
                        type: 2,
                        title: '鲜花维护',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['580px', '90%'],
                        content: '/pages/flType/form.html' //iframe的url
                    });
                    break;
                case 'update':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else if(data.length > 1){
                        layer.msg('只能同时编辑一个');
                    } else {
                        //layer.alert('编辑 [id]：'+ JSON.stringify(checkStatus.data[0]));
                        layer.open({
                            type: 2,
                            title: '鲜花类型维护',
                            shadeClose: true,
                            shade: 0.8,
                            area: ['580px', '90%'],
                            content: '/pages/flType/form.html?id='+checkStatus.data[0].typeId //iframe的url
                        });
                    }
                    break;
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        //layer.msg('删除');
                        layer.confirm('真的删除行么', function(index) {
                            layer.close(index);//关闭当前弹窗
                            let ids = "";
                            for (let i = 0; i < data.length; i++) {
                                ids += data[i].typeId + ",";
                            }
                            console.info("1===================" + ids);
                            ids = ids.substring(0, ids.length - 1);
                            console.info("2===================" + ids);
                            let url = "/flType/deleteBatch";
                            let data_ = {ids: ids};
                            $.post(url, data_, function (resp) {
                                if (resp.state == 1) {
                                    layer.msg(resp.msg);
                                    //删除完成之后重新加载表格
                                    table.reload('testTable');
                                } else {
                                    layer.msg("删除失败!");
                                }
                            }, "json");
                        });
                    }

                    break;
            };
        });

    });

    /**
     * 关闭层的方法
     */
    function closeLayer(msg){
        layer.closeAll();//关闭所有弹出的层
        table.reload('testTable');
        layer.msg(msg);
    }
</script>
</body>
</html>